﻿using FlappyGame.View.ViewEx;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;

namespace FlappyGame.View
{
    internal class Poles : IDisposable
    {
        const int WIDTH = 70;
        const int Height = 20;
        const int Padding = 210;
        const int left = 3;
        readonly Color startColor = Color.FromArgb(79, 132, 33);
        readonly Color endColor = Color.FromArgb(219, 255, 130);
        Pen _penBlack = new Pen(Color.Black, 2);
        Random _ran = new Random();
        public void Dispose()
        {
            _penBlack.Dispose();
        }
        public void DrawGradientTop(Graphics g, Rectangle rect)
        {
            var bottomRect = new Rectangle(rect.X - left, rect.Y + rect.Height, rect.Width + (left << 1), Height);
            DrawGradient(g, rect, bottomRect);
        }
        public void DrawGradientBottom(Graphics g, Rectangle rect)
        {
            var bottomRect = new Rectangle(rect.X - left, rect.Y - Height, rect.Width + (left << 1), Height);

            DrawGradient(g, rect, bottomRect);
        }
        private void DrawGradient(Graphics g, Rectangle rect, Rectangle rect2)
        {
            Point startPoint = new Point(rect.Left, rect.Top);
            Point endPoint = new Point(rect.Right, rect.Top);
            using (LinearGradientBrush brush = new LinearGradientBrush(
                startPoint, endPoint, startColor, endColor))
            {
                g.FillRectangle(brush, rect);
            }
            g.DrawRectangle(_penBlack, rect);

            var bottomRect = rect2;
            startPoint = new Point(bottomRect.Left, bottomRect.Top);
            endPoint = new Point(bottomRect.Right, bottomRect.Top);
            using (LinearGradientBrush brush = new LinearGradientBrush(
               startPoint, endPoint, startColor, endColor))
            {
                g.FillRectangle(brush, bottomRect);
            }
            g.DrawRectangle(_penBlack, bottomRect);
        }
        const int PH = 250;
        public Pole GetPole(int x, int maxHeight)
        {
            var p = new Pole();
            int h = _ran.Next(60, 130);
            int w = _ran.Next(0, 1);
            p.Top = new Rectangle(x, 0, WIDTH, PH + (w == 0 ? -h : h));
            int nh = maxHeight - p.Top.Height - Padding + (w == 0 ? h : -h);
            p.Bottom = new Rectangle(x, p.Top.Height + Padding, WIDTH, nh);
            return p;
        }
        /// <summary>
        /// 判断碰撞
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool IsCollision(int x, int y, int birdSize, List<Pole> data)
        {
            // 定义小鸟的矩形
            var rectBird = new Rectangle(x, y, birdSize, birdSize);

            foreach (var pole in data)
            {
                // 定义顶部和底部障碍物的矩形
                var bottomRect = new Rectangle(pole.Bottom.X - left, pole.Bottom.Y - Height, pole.Bottom.Width + (left << 1), Height);
                var topRect = new Rectangle(pole.Top.X - left, pole.Top.Y + pole.Top.Height, pole.Top.Width + (left << 1), Height);

                // 检查小鸟是否与任何障碍物矩形相交
                if (rectBird.IntersectsWith(bottomRect) || rectBird.IntersectsWith(topRect))
                {
                    return true;
                }
            }

            return false;
        }
    }
    public class Pole
    {
        public Rectangle Top { get; set; }
        public Rectangle Bottom { get; set; }

        public void Change(int step)
        {
            Top = new Rectangle(Top.X - step, Top.Y, Top.Width, Top.Height);
            Bottom = new Rectangle(Bottom.X - step, Bottom.Top, Bottom.Width, Bottom.Height);
        }
    }
}
